package org.homeworkteam.utils;

import org.json.JSONObject;
import org.homeworkteam.dto.AiKey;

import okhttp3.*;

import java.io.*;

import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Base64;
import java.net.URLEncoder;

public class AiUtils {

    private static final OkHttpClient HTTP_CLIENT = new OkHttpClient().newBuilder().build();
    private static final MediaType MEDIA_TYPE_FORM = MediaType.parse("application/x-www-form-urlencoded");
    private static final MediaType MEDIA_TYPE_JSON = MediaType.parse("application/json");

    private static final AiKey aiKey = new AiKey();


    //    图片转 base64 编码
    public static String encodeFileToBase64(String path, boolean urlEncode) throws IOException {
        byte[] b = Files.readAllBytes(Paths.get(path));
        String base64 = Base64.getEncoder().encodeToString(b);
        if (urlEncode) {
            base64 = URLEncoder.encode(base64, "utf-8");
        }
        return base64;
    }

    //    作业批改
    //    向ai发送请求 获取请求体
    public static String sendPostRequest(String url, String jsonBody) throws IOException {
        RequestBody body = RequestBody.create(MEDIA_TYPE_JSON, jsonBody);
        Request request = new Request.Builder()
                .url(url + getCorrectionAccessToken())
                .method("POST", body)
                .addHeader("Content-Type", "application/json")
                .build();
        Response response = HTTP_CLIENT.newCall(request).execute();
        return response.body().string();
    }

    //    作业批改的access_token
    //    使用APIKey与 SecretKey 获取access_token
    public static String getCorrectionAccessToken() throws IOException {
//        调用作业批改的两个API
        RequestBody body = RequestBody.create(MEDIA_TYPE_FORM, "grant_type=client_credentials&client_id=" + aiKey.getCorrectionAPIKey()
                + "&client_secret=" + aiKey.getCorrectionSecretKey());
        Request request = new Request.Builder()
                .url("https://aip.baidubce.com/oauth/2.0/token")
                .method("POST", body)
                .addHeader("Content-Type", "application/x-www-form-urlencoded")
                .build();
        Response response = HTTP_CLIENT.newCall(request).execute();
        return new JSONObject(response.body().string()).getString("access_token");
    }

//    文本审核
    public static String sendReviewPost(String urlTop,String urlBottom,String type,String content) throws IOException {
        RequestBody body = RequestBody.create(MEDIA_TYPE_FORM, content);
//        拼接完整url  走对应的URL
        String url = urlTop + type + urlBottom;
        Request request = new Request.Builder()
                .url( url + getReviewAccessToken())
                .method("POST",body)
                .addHeader("Content-Type", "application/x-www-form-urlencoded")
                .addHeader("Accept","application/json")
                .build();
        Response response = HTTP_CLIENT.newCall(request).execute();
        String result = response.body().string();
        System.out.println("Utils"+result);
        return result;
    }

    public static String getReviewAccessToken() throws IOException {
        RequestBody body = RequestBody.create(MEDIA_TYPE_FORM,"grant_type=client_credentials&client_id="
                + aiKey.getReviewAPIKey() + "&client_secret=" + aiKey.getReviewSecretKey() );
        Request request = new Request.Builder()
                .url("https://aip.baidubce.com/oauth/2.0/token")
                .method("POST", body)
                .addHeader("Content-Type", "application/x-www-form-urlencoded")
                .build();
        Response response = HTTP_CLIENT.newCall(request).execute();
        return new JSONObject(response.body().string()).getString("access_token");
    }
}